/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package proteindomainparser;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author nadaalhirabi
 */
public class ProteinDomainParser {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        
        
        // reading the file names from the user
        Scanner User_input=new Scanner(System.in);
        String Dom_file_name, Complete_file;
        

        System.out.print("\nEnter the Domain file name:");
        Dom_file_name=User_input.next();
   
        
        //---------------- parsing domain file
        
     String domtoken = "";    
     List<String> Domtemps;
     int domlength=0;
        try (Scanner inFile = new Scanner(new File("A_acidus_CBS_106_47_iprscan.out.txt")).useDelimiter("\n")) {
            Domtemps = new LinkedList<>();
            while (inFile.hasNext()) {
                // find next line
                domtoken = inFile.next();       
                Domtemps.add(domtoken);
                domlength++;
            }   }

    String[] tempsDomArray = Domtemps.toArray(new String[0]); 
    String[] Domdigested = new String[domlength];
    
    int j=0;
    for (String st : tempsDomArray) {//loop for all the file// each index of tempsArray array is line // so we extract line by line
        int index=0;
         for (String retvalDom: st.split("\t")){//loop for each line to extract the desired feature
           
             if (index==0){ // to take the id of the dom
                     String ProId= retvalDom;
                     Domdigested[j]=ProId+ "\t";
                     // System.out.println(ProId);
             }
             else if (index==2){ // to take the length of the protein
                     String Prolength= retvalDom;
                     Domdigested[j]+=Prolength+ "\t";
                     //System.out.println(Prolength);
             }
              
            else  if (index==6){ // to take the start of the feature
                     String Domstart= retvalDom;
                     Domdigested[j]+=Domstart+"\t";
                     //System.out.println(Domstart);
             }
              
             else  if (index==7){
                     String Domend= retvalDom; // to take the end of the feature
                     Domdigested[j]+=Domend + "\t";
                     //System.out.println(Domend);
             }   
               
              else    if (index==11){
                     String InterProID= retvalDom; // to take the InterPro domain identifier
                     Domdigested[j]+=InterProID+ "   ";
                     //System.out.println(InterProID);
             } 
                  
               else  if (index==12){
                     String InterProDescript= retvalDom; // to take the InterPro domain description
                     Domdigested[j]+=InterProDescript ;
                     //System.out.println(InterProID);
             } 
               index++;
             } //end the loop for each line to extract the desired feature
         
         ++j;     
      }//end the loop for whool the array that hold the file
    
    writeTofile( Domdigested, Dom_file_name+"ExtractedIprScanOutputFile");

    }//end main
 
    
    public static void writeTofile(String[] digestedfile,  String filename) throws FileNotFoundException, IOException{
    
//System.out.println(digestedfile[1]);  

BufferedWriter writer = null;
try {
    File file = new File(filename+".txt");  
    PrintWriter out = new PrintWriter(new FileWriter(file));  

    // Write each string in the array on a separate line  
    for ( int i=0; i<digestedfile.length ;++i) {  
        String s =digestedfile[i];
        out.println(s);  
       //if(s.equals("##END"))break;
    }  
    out.close();  
    } catch(IOException ex) {
        ex.printStackTrace();
    }  
  
}//end write to file
    
}